﻿using IntelligentStorWpfApp.Controller;
using IntelligentStorWpfApp.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// search.xaml 的交互逻辑
    /// </summary>
    public partial class Search : Window
    {
        public Search()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        private ProductController prodControl;
        private LinkProductPositionController lppControl;
        private List<ProductInfo> listPrds;
        
        private void SearchWindow_Loaded(object sender, RoutedEventArgs e)
        {
            prodControl = new ProductController();
            lppControl = new LinkProductPositionController();
            listPrds = new List<ProductInfo>();
            listPrds = prodControl.getAllProducts(new ProductInfo());
            bindingData(App.Num, 1);
            //this.dgProducts.SelectedIndex = 0;
        }

        private void dgProducts_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            ProductInfo product = new ProductInfo();
            product.ProductId = "%"+txtProductId.Text.Trim()+"%";
            listPrds= prodControl.getAllProducts(product);
            bindingData(App.Num, 1);
        }

        private void btnInput_Click(object sender, RoutedEventArgs e)
        {
            int index = dgProducts.SelectedIndex;
            ProductInfo prdInfo = dgProducts.SelectedItem as ProductInfo;
            if(!string.IsNullOrEmpty(prdInfo.Pposition))
            {
                addContext(false,prdInfo);
            }
            else
            {
                addContext(true,prdInfo);
            }
        }

    private void addContext(bool value,ProductInfo prodInfo)
        {
            ContextMenu context = new ContextMenu();
            MenuItem item ;

            item = new MenuItem();
            item.Name = "miInput";
            item.Header = "立即入库/出库";
            item.Tag = prodInfo;
            item.Click += miInput_Click;
            context.Items.Add(item);
            item = new MenuItem();
            item.Name = "miIntelliInput";
            item.Header = "智能入库/出库";
            item.Tag = prodInfo;
            item.Click += miIntelliInput_Click;
            context.Items.Add(item);
            context.IsOpen = true;
        }

        private void miIntelliInput_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;
            Setting st = new Setting();
            try
            {

                st.productInfo = item.Tag as ProductInfo;
                LinkProductPosition draftlinkProd = lppControl.GetDraftLinkByProductId(st.productInfo.ProductId);
                if (draftlinkProd != null)
                {
                    MessageBoxResult msResult = MessageBox.Show("该产品已经设定在" + draftlinkProd.SetTime.ToString("yyy-MM-dd HH:mm:ss") + " " + draftlinkProd.Action + "是否重新设定?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (msResult == MessageBoxResult.Yes)
                    {
                        lppControl.deleteDraftLink(draftlinkProd.ID);
                    }
                    else
                    {
                        return;
                    }
                }
                st.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            
        }

        private void miInput_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;
            ProductInfo pi = item.Tag as ProductInfo;
            try
            {

                if (string.IsNullOrEmpty(pi.Pposition))
                {
                    CabinetWindow mwin = new CabinetWindow();
                    mwin.productId = pi.ProductId;
                    mwin.ShowDialog();
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("是否立即出库?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        if (lppControl.deleteLinkByProduct(pi.ProductId))
                        {
                            MessageBox.Show(pi.ProductId + "出库成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);

                        }
                        else
                        {
                            MessageBox.Show(pi.ProductId + "出库失败!请稍后再试", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                    listPrds = prodControl.getAllProducts(new ProductInfo());
                    bindingData(App.Num, 1);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
        //number表示每个页面显示的记录数    currentSize表示当前显示页数
        private void bindingData(int number, int currentSize)
        {
            List<ProductInfo> infoList = new List<ProductInfo>();
            infoList = listPrds;      //获取数据源
            int count = infoList.Count;          //获取记录总数
            int pageSize = 0;            //pageSize表示总页数
            if (count % number == 0)
            {
                pageSize = count / number;
            }
            else
            {
                pageSize = count / number + 1;
            }
            tbkTotal.Text = pageSize.ToString();

            tbkCurrentsize.Text = currentSize.ToString();
            infoList = infoList.Take(number * currentSize).Skip(number * (currentSize - 1)).ToList();   //刷选第currentSize页要显示的记录集
            dgProducts.ItemsSource = null;
            dgProducts.ItemsSource = infoList;        //重新绑定dataGrid1
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            int currentsize = int.Parse(tbkCurrentsize.Text); //获取当前页数
            if (currentsize > 1)
            {
                bindingData(App.Num, currentsize - 1);   //调用分页方法
            }
        }
        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            int pageNum = int.Parse(tbxPageNum.Text);
            int total = int.Parse(tbkTotal.Text); //总页数
            if (pageNum >= 1 && pageNum <= total)
            {
                bindingData(App.Num, pageNum);     //调用分页方法
            }
        }
        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            int total = int.Parse(tbkTotal.Text); //总页数
            int currentsize = int.Parse(tbkCurrentsize.Text); //当前页数
            if (currentsize < total)
            {
                bindingData(App.Num, currentsize + 1);   //调用分页方法
            }
        }
    }

}
